package fourfaith.com.ff_sl21_app.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.util.Log;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

public class DatePickerDialogUtil {

    /**
     * 日期控件简单封装
     *
     * @param activity
     * @param year        控件初始 年
     * @param monthOfYear 控件初始 月
     * @param dayOfMonth  控件初始 日
     * @param obj         结果接收控件
     * @return
     */
    public static DatePickerDialog initDialog(Activity activity, int year, int monthOfYear, int dayOfMonth,
                                              final EditText obj) {

        DatePickerDialog dialog = null;
        //android.R.style.Theme_Material_Light_Dialog_Alert
        dialog = new DatePickerDialog(activity, AlertDialog.THEME_HOLO_LIGHT, null, year, monthOfYear, dayOfMonth);

        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                DatePicker datePicker = ((DatePickerDialog) dialog).getDatePicker();

                StringBuilder sb = new StringBuilder();
                sb.append(datePicker.getYear());
                sb.append("-");

                if (datePicker.getMonth() < 9) {
                    sb.append("0" + (datePicker.getMonth() + 1));
                } else {
                    sb.append(datePicker.getMonth() + 1);
                }

                sb.append("-");
                if (datePicker.getDayOfMonth() < 10) {
                    sb.append("0" + datePicker.getDayOfMonth());
                } else {
                    sb.append(datePicker.getDayOfMonth());
                }
                obj.setText(sb.toString());

            }
        });
        dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Log.d("Picker", "Cancel!");
            }
        });

        return dialog;
    }

    /**
     * 日期控件
     * @param activity
     * @param year
     * @param monthOfYear
     * @param dayOfMonth
     * @param obj
     * @return
     */
    public static DatePickerDialog initDialog(Activity activity, int year, int monthOfYear, int dayOfMonth,
                                              final TextView obj) {

        DatePickerDialog dialog = null;
        //android.R.style.Theme_Material_Light_Dialog_Alert
        dialog = new DatePickerDialog(activity, AlertDialog.THEME_HOLO_LIGHT, null, year, monthOfYear, dayOfMonth);

        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                DatePicker datePicker = ((DatePickerDialog) dialog).getDatePicker();

                StringBuilder sb = new StringBuilder();
                sb.append(datePicker.getYear());
                sb.append("-");

                if (datePicker.getMonth() < 9) {
                    sb.append("0" + (datePicker.getMonth() + 1));
                } else {
                    sb.append(datePicker.getMonth() + 1);
                }

                sb.append("-");
                if (datePicker.getDayOfMonth() < 10) {
                    sb.append("0" + datePicker.getDayOfMonth());
                } else {
                    sb.append(datePicker.getDayOfMonth());
                }
                obj.setText(sb.toString());

            }
        });
        dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Log.d("Picker", "Cancel!");
            }
        });

        return dialog;
    }

}
